import Api from '../../utils/api.js';
import {
  checkRequire,
  checkNumber,
  checkNumberBigger,
  checkLater,
  checkSameDay
} from '../../utils/validator.js';
import prompt from '../../utils/prompt.js';

const app = getApp();

// components/stayForm/stayForm.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    checkinTargetList: ['公司领导', '分公司领导', '政府领导', '培训导师'], // 入住对象列表
    dheckinLocationList: ['会理', '西昌', '攀枝花'], // 入住地点列表

    isFruitSnack: false, // 是否配备水果点心
    checkinLocation: '', // 入住地点
    checkoutTime: '', // 退房时间
    checkinDateTime: '', // 入住时间
    roomsCount: '', // 房间数
    checkinCount: '', // 入住人数
    checkinTarget: '', // 入住对象

    ps_checkinLocation: true,
    ps_checkoutTime: true,
    ps_checkinDateTime: true,
    ps_roomsCount: true,
    ps_checkinCount: true,
    ps_checkinTarget: true
  },

  /**
   * 组件的方法列表
   */
  methods: {

    /* --===================数据接口===================-- */
    // 提交住宿申请
    async submitRoom () {
      const param = {
        'checkIn': this.data.checkinTarget,
        'checkInLoc': this.data.checkinLocation,
        'checkInNum': this.data.checkinCount,
        'checkInTime': this.data.checkinDateTime,
        'checkOutTime': this.data.checkoutTime || '',
        'fruitSnack': this.data.isFruitSnack ? '1' : '0',
        'roomsNum': this.data.roomsCount
      };
      const res = await Api.applyRoom(param, app.globalData.sessionKey);
      if (res && res.retCode === '0') {
        prompt.showToast('申请住宿成功', 'success', { mask: true });
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    },

    /* --===================功能函数===================-- */
    // 表单检查
    checkInput () {
      let ps_checkinTarget = false;
      let ps_checkinCount = false;
      let ps_roomsCount = false;
      let ps_checkinDateTime = false;
      let ps_checkoutTime = true; // 可选，退房时间
      let ps_checkinLocation = false;

      ps_checkinTarget = checkRequire(this.data.checkinTarget, '请选择或输入入住对象');
      this.setData({ ps_checkinTarget });

      if (ps_checkinTarget) {
        ps_checkinCount = (
          checkRequire(this.data.checkinCount, '请输入入住人数') &&
          checkNumber(this.data.checkinCount, '请输入正确的入住人数') &&
          checkNumberBigger(this.data.checkinCount, 1, '入住人数最少为1人')
        );
        this.setData({ ps_checkinCount });
      }

      if (ps_checkinCount) {
        ps_roomsCount = (
          checkRequire(this.data.roomsCount, '请输入房间数') &&
          checkNumber(this.data.roomsCount, '请输入正确的房间数') &&
          checkNumberBigger(this.data.roomsCount, 1, '房间数最少为1间')
        );
        this.setData({ ps_roomsCount });
      }

      if (ps_roomsCount) {
        const current = new Date();
        ps_checkinDateTime = (
          checkRequire(this.data.checkinDateTime, '请选择入住时间') &&
          checkLater(new Date(this.data.checkinDateTime), current, '入住时间不可早于当前')
        );
        this.setData({ ps_checkinDateTime });
      }

      if (ps_checkinDateTime) {
        if (this.data.checkinDateTime && this.data.checkoutTime) {
          const checkInDate = new Date(this.data.checkinDateTime);
          const checkOutDate = new Date(this.data.checkoutTime);
          ps_checkoutTime = (
            checkLater(checkOutDate, checkInDate, '退房时间不可早于入住时间') &&
            !checkSameDay(checkInDate, checkOutDate, '请至少入住一晚')
          );
          this.setData({ ps_checkoutTime });
        }
      }

      if (ps_checkoutTime && ps_checkinDateTime) {
        ps_checkinLocation = checkRequire(this.data.checkinLocation, '请选择或输入入住地点');
        this.setData({ ps_checkinLocation });
      }

      return (
        ps_checkinTarget &&
        ps_checkinCount &&
        ps_roomsCount &&
        ps_checkinDateTime &&
        ps_checkoutTime &&
        ps_checkinLocation
      );
    },

    /* --===================事件处理===================-- */
    // 数输入事件
    handleInput (e) {
      const param = {};
      param[e.currentTarget.dataset.state] = e.detail.value;
      this.setData(param);
    },
    // 获取 picker input 组件值
    handlePickerInputValue (e) {
      const param = {};
      param[e.currentTarget.dataset.value] = e.detail.value;
      this.setData(param);
    },
    // 取消时间日期选择
    handleCancel (e) {
      const param = {};
      param[e.currentTarget.dataset.value] = '';
      console.log(param);
      this.setData(param);
    },
    // 表单提交时间
    handleSubmit () {
      if (this.checkInput()) {
        this.submitRoom();
      }
    }
  }
});
